package com.oliet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.oliet.common.vo.permission.PermissionTreeNodeVO;
import com.oliet.common.vo.permission.PermissionVO;
import com.oliet.entity.Permissions;
import com.oliet.entity.RolePermissions;
import com.oliet.entity.base.BaseEntity;
import com.oliet.mapper.RolePermissionsMapper;
import com.oliet.service.PermissionsService;
import com.oliet.service.RolePermissionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JZP
 * @since 2023-12-09
 */
@Service
public class RolePermissionsServiceImpl extends ServiceImpl<RolePermissionsMapper, RolePermissions> implements RolePermissionsService {

    @Autowired
    PermissionsService permissionsService;

    @Override
    public void deleteByRoleId(Long roleId) {
        this.remove(new LambdaQueryWrapper<RolePermissions>().eq(RolePermissions::getRoleId, roleId));
    }

    @Override
    public void batchInsert(Long id, List<Long> permissionIds) {
        List<RolePermissions> rolePermissionsList = permissionIds.stream().map(permissionId -> {
            RolePermissions rolePermissions = new RolePermissions();
            rolePermissions.setRoleId(id);
            rolePermissions.setPermissionId(permissionId);
            return rolePermissions;
        }).collect(Collectors.toList());
        this.saveBatch(rolePermissionsList);
    }

    @Override
    public List<PermissionVO> getPermissionVoByRoleId(Long roleId) {
        List<PermissionVO> result = Lists.newArrayList();
        List<RolePermissions> rolePermissionsList = this.list(new LambdaQueryWrapper<RolePermissions>().eq(RolePermissions::getRoleId, roleId));
        if (CollectionUtil.isEmpty(rolePermissionsList)){
            return result;
        }
        List<Long> permissionIds = rolePermissionsList.stream().map(it -> it.getPermissionId()).distinct().collect(Collectors.toList());
        List<Permissions> permissionsList = this.permissionsService.list(new LambdaQueryWrapper<Permissions>().in(BaseEntity::getId, permissionIds));
        return PermissionVO.poListConverVoList(permissionsList);
    }

    @Override
    public List<PermissionTreeNodeVO> getPermissionTreeByRoleIds(List<Long> roleIds) {
        List<PermissionTreeNodeVO> result = Lists.newArrayList();
        List<RolePermissions> rolePermissionsList = this.list(new LambdaQueryWrapper<RolePermissions>().in(RolePermissions::getRoleId, roleIds));
        if (CollectionUtil.isEmpty(rolePermissionsList)){
            return result;
        }
        List<Long> permissionIds = rolePermissionsList.stream().map(it -> it.getPermissionId()).distinct().collect(Collectors.toList());
        List<Permissions> permissionList = this.permissionsService.list(new LambdaQueryWrapper<Permissions>().in(BaseEntity::getId, permissionIds));
        return PermissionTreeNodeVO.buildTree(permissionList);
    }
}
